package edu.sctu.zhoumengxue.day201506.day20150615.bufferReader;

import java.io.FileReader;
import java.io.IOException;

/**
 * Created by user12 on 2015/6/15.
 */
public class MyBuffer {

    private FileReader file;
    private char buffer[] = new char[1024];
    private int count = 0;
    private int position = 0;

    public MyBuffer(FileReader file){
        this.file = file;
    }

    public int myRead (){

        if (count==0) {
            try {
                count = file.read(buffer);
                position = 0;

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            if (count<0)
                return -1;
            int ch=buffer[position++];
            count--;
            return ch;

    }

    public String myReadLine(){
        StringBuilder builder = new StringBuilder();
        int ch = 0;
        while ((ch=myRead())!=-1){
            if(ch=='\r')
                continue;
            if (ch=='\n')
                return builder.toString();
            builder.append((char) ch);
            if(count==0)
                return builder.toString();
        }
        return null;
    }

    public void myClose(){
        try {
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
